<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * listBS controller
 * @author Phuc Nguyen - 09/03/2015
 * http://www.facebook.com/phucthuong
 * email : nguyenvanphuc0626@gmail.com
*/
class ListBS extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->language('listBS');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('listBS_model');
			$this->set_controller('listBS');
			$this->set_model($this->listBS_model);
		}
                
                $this->load->library('bookinglib');
                $this->booking = new bookinglib();
	}
        
        private function menuTop(){
            $this->load->model('stats_model');
            $user_info                          = $this->stats_model->get_user_info($this->stats_model->get_user_id());
            $this->view_data['role_by_group']   = $user_info['role_list'];
            $this->view_data['username']        = $user_info['username'];
            return $this->view_data;
        }
        
        public function lists(){
            $this->menuTop();
            if($this->input->server('REQUEST_METHOD')=='POST'){
                $data = array(
                    'begin'     =>  ($this->input->post('beginTime'))?strtotime($this->input->post('beginTime')):'',
                    'end'       =>  ($this->input->post('endTime'))?strtotime($this->input->post('endTime')):''
                );
                
                $this->load->model('listcustomers_model');
                $this->load->model('user_model');
                
                $lists                              = $this->listcustomers_model->selectBS($data);
                $info                               = array();

				$this->load->model('sales_model');
				
                foreach($lists as $keys => $vals){
                    $salers     = $this->user_model->getOwnerFromTblUser($vals->salesID);
                    $bookers    = $this->user_model->getOwnerFromTblUser($vals->owner);
					//ngay sale
					$phone		=	json_decode($vals->phone);
					$dateSale	= $this->sales_model->find_by(array('phone'=>$phone[0]));
                    
                    $info[$keys]  = array(
                        'name'              =>  $vals->name,
                        'phone'             =>  implode(",",json_decode($vals->phone)),
                        'saler'             =>  $salers[0]->firstname,
                        'booker'            =>  $bookers[0]->firstname,
						'dateSale'			=>	date("d-m-Y H:i:s",$dateSale[0]->lastupdated),
                        'sumMoney'          =>  $this->bookinglib->my_number_format($vals->totalMoneyBooking,2, ',', ',').'VNĐ'
                    );
                }
                $this->view_data['lists'] = $info;
            }
            $this->load->view('auth/listBS/lists', $this->view_data);
        }
}